<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BMediaManager
 *
 * @author I062261
 */
class BSMediaManager {

    //put your code here
    public $owner;
    //put your code here
    protected $mediaList = array();
    private $loaded = false;

    public function __construct($owner) {
        if ($owner instanceof BEntity)
            $this->owner = $owner;
    }

    public function addMedia($media, $data = NULL) {
        return $this->saveMedia($media);
    }

    public function removeMedia($media) {

        if ($media instanceof BSMedia) {
            foreach ($this->mediaList as $name => $aMedia) {
                if ($aMedia instanceof BSMedia) {
                    if ($aMedia->getId() == $media->getId()) {
                        unset($this->mediaList[$media->getId()]);
                    }
                }
            }
        }

        if (is_string($media)) {
            foreach ($this->mediaList as $id => $aMedia) {
                if ($aMedia->getName() == $media)
                    unset($this->mediaList[$id]);
            }
        }
    }

    public function getMedia($identity) {
        if (is_numeric($identity)) {
            foreach ($this->mediaList as $id => $media) {
                if ($id == $identity)
                    return $media;
            }
        }
        if (is_string($identity)) {
            $name = $identity;
            $list = $this->getMediaList();
            foreach ($list as $media) {
                if ($media->getName() == $name)
                    return $media;
            }
        }
        return FALSE;
    }

    public function getMediaList($data = NULL) {
        if (!$this->loaded)
            $this->loadMediaList();
        return $this->mediaList;
    }

    public function setMediaList($mediaList) {
        if (is_array($mediaList))
            $this->mediaList = $mediaList;
    }

    /**
     * this function save media into database
     * @param type $data
     */
    public function saveMediaList($data = NULL) {
        if ($this->owner instanceof BSEntity) {
            foreach ($this->mediaList as $id => $media) {
                $this->saveMedia($media);
            }
        }
        return FALSE;
    }

    protected function saveMedia($media) {
        $attributes = array();
        $attributes['resource_id'] = $this->owner->getId();
        $attributes['resource_type'] = get_class($this->owner);
        //if(isset($media->name) && $media->name != NULL)
        // $attributes['name'] = $media->name;
        //$attributes['media_id'] = $id;
        $findResourceMediaList = BSMResourceMedia::model()->findAllByAttributes($attributes);
        foreach ($findResourceMediaList as $findResourceMedia) {
            if ($findResourceMedia instanceof CActiveRecord) {
                //if media saved already, just modify media
                $mediaId = $findResourceMedia->media_id;
                //once fine exist, just save it
                if ($media instanceof BSMedia) {
                    if ($media->getId() == $mediaId) {
                        $media->save();
                        return TRUE;
                    }
                }
            }
        }
        //else { //if not find in database, save media first and then add record into resourcemedia table
        if ($media instanceof BSMedia && $this->owner instanceof BSEntity) {
            $media->save();
            $id = $media->getId();
            $row = new BSMResourceMedia();
            $row->resource_id = $this->owner->getId();
            $row->media_id = $id;
            $row->resource_type = get_class($this->owner);
            $row->save();
        }
        //}
    }

    /**
     * this function load medialist from database
     * @param type $data
     */
    public function loadMediaList($data = NULL) {
        $attributes = array();
        $attributes['resource_id'] = $this->owner->getId();
        $attributes['resource_type'] = get_class($this->owner);
        $rowList = BSMResourceMedia::model()->findAllByAttributes($attributes);
        foreach ($rowList as $row) {
            $mediaId = $row->media_id;
            $this->mediaList[$mediaId] = new BSMedia($mediaId);
        }
        $this->loaded = TRUE;
    }

}

?>
